package flatlandAgent;

public class FlatlandMap {
	
	private char[][] map; 
	
	public final static int mapSize= 8; 
	private double foodProbability; 
	private double poisonProbability;
	private int countFood, countPoison, countNone; 
	
	public FlatlandMap(double foodProbability, double poisonProbability) {
		super();
		this.foodProbability = foodProbability;
		this.poisonProbability = poisonProbability;
		createMap(); 
	} 
	
	private void createMap(){
		int countFood = 0; 
		int countPoison = 0; 
		int countNone = 0; 
		map = new char[mapSize][mapSize]; 
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				if (Math.random()<foodProbability){
					map[i][j] = 'f'; 
					countFood ++;
				} else if(Math.random() < poisonProbability){
					map[i][j] = 'p';
					countPoison ++; 
				} else{
					map[i][j] = '.';
					countNone ++; 
				}
			}
		}
		this.countFood = countFood; 
		this.countPoison = countPoison; 
		this.countNone = countNone; 
	}
	
	public FlatlandMap copy(){
		FlatlandMap copy = new FlatlandMap(0, 0); 
		for (int i = 0; i < map.length; i++){
			for (int j = 0; j < map[i].length; j++){
				copy.setCell(i, j, this.getCell(i, j)); 
			}
		}
		return copy; 
	}
	
	public void setCell(int x,int y, char c){
		x = modulus(x, mapSize); 
		y = modulus(y, mapSize);
		map[x][y] = c;
	}
	
	public char getCell(int x, int y){
		x = modulus(x, mapSize); 
		y = modulus(y, mapSize);
		return map[x][y]; 
	}
	
	public int getSize(){
		return mapSize; 
	}
	
	private static int modulus(int a, int b){
		while (a < 0) a += b;
		return a%b;
	}
	
	public int getCountFood() {
		return countFood;
	}

	public int getCountPoison() {
		return countPoison;
	}

	public int getCountNone() {
		return countNone;
	}

	public String toString(){
		StringBuilder stringBuilder = new StringBuilder(mapSize*mapSize + mapSize*2); 
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				stringBuilder.append(this.getCell(i,j)); 
			}
			stringBuilder.append("\n");
		}
		return stringBuilder.toString(); 
	}

}
